﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Security.Cryptography;

namespace oop_5_server
{
    struct sUser
    {
        public string userkey;
        public string username;
    }

    class UserManager
    {
        protected ArrayList Users;

        public ArrayList List
        {
            get { 
                return Users;
            }
        }

        public UserManager()
        {
            Users = new ArrayList();
        }

        public Packet Login (ref Dictionary<string, string> query) {
            Packet resp = new Packet();

            string hash = this.GetMd5Hash(query["datetime"]);
            hash = hash.Substring(hash.Length - 5);

            sUser nuser = new sUser { userkey = hash, username = string.Format("User-{0}", hash) };

            resp.Add("entity", "user");
            resp.Add("username", nuser.username);
            resp.Add("userkey", nuser.userkey);
            resp.Add("success", "true");

            Users.Add(nuser);

            return resp;
        }

        public Packet Logout (ref Dictionary<string, string> query)
        {
            Packet resp = new Packet();

            sUser duser = new sUser { username = "", userkey = "" };
            foreach (sUser user in Users)
            {
                if (user.userkey == query["userkey"])
                {
                    duser = user;
                    break;
                }
            }

            Users.Remove(duser);
            return resp;
        }

        public Packet Rename (ref Dictionary<string, string> query)
        {
            Packet resp = new Packet();

            sUser duser = new sUser { username = "", userkey = "" };
            foreach (sUser user in Users)
            {
                if (user.userkey == query["userkey"])
                {
                    duser = user;
                    break;
                }
            }

            Users.Remove(duser);

            sUser nuser = new sUser { userkey = duser.userkey, username = query["user"] };

            resp.Add("entity", "user");
            resp.Add("success", "true");
            resp.Add("username", nuser.username);

            Users.Add(nuser);

            return resp;
        }

        public Packet GetUserList(ref Dictionary<string, string> query)
        {
            Packet resp = new Packet();

            resp.Add("entity", "user");
            resp.Add("success", "true");

            Packet usersResp = new Packet();
            int userRespK = 1;
            foreach (sUser user in Users)
            {
                Packet userResp = new Packet();
                userResp.Add("username", user.username);
                userResp.Add("userkey", user.userkey);
                usersResp.Add((userRespK++).ToString(), userResp);
            }

            resp.Add("users", usersResp);

            return resp;
        }

        public string GetUsernameByKey(string key)
        {
            foreach (sUser user in Users)
            {
                if (user.userkey == key)
                {
                    return user.username;
                }
            }

            return "";
        }


        private string GetMd5Hash(string input)
        {

            byte[] data = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(input));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }
    }
}
